﻿Imports System.IO
Imports System.Xml
Module XMLStuff

    Public Sub CheckXMLIntegrity(ByVal xmlLocation As String)
        Dim br As BinaryReader = New BinaryReader(File.Open(xmlLocation, FileMode.Open), System.Text.Encoding.BigEndianUnicode())
        br.BaseStream.Seek(br.BaseStream.Length - 1, SeekOrigin.Begin)

        If Hex(br.ReadByte) <> "20" Then
            br.Close()
            Dim temp As String
            Dim filestream As StreamReader = New StreamReader(xmlLocation)
            temp = filestream.ReadToEnd().ToString
            Do Until temp.Last = ">"
                temp = temp.Remove(temp.Length - 1).ToString
            Loop
            filestream.Close()
            My.Computer.FileSystem.WriteAllText(xmlLocation, temp, False)
            'xmlLocation = "c:/temp.xml"
            'Dim wr As BinaryWriter = New BinaryWriter(File.Open(xmlLocation, FileMode.Open), System.Text.Encoding.BigEndianUnicode())
            'wr.BaseStream.Seek(wr.BaseStream.Length - 1, SeekOrigin.Begin)
            'wr.Write(".")
            'wr.Close()
        Else
            br.Close()
        End If
    End Sub

    Public Sub InitialXMLRead(ByVal xmlLocation As String)
        Dim oRead As System.IO.StreamReader
        oRead = File.OpenText(xmlLocation)

        Dim xmlstream As String

        xmlstream = oRead.ReadToEnd
        oRead.Close()

        If xmlstream.Contains("&") Then
            xmlstream = xmlstream.Replace("&", "and")
        End If

        My.Computer.FileSystem.WriteAllText("temp_content.xml", xmlstream, False)
        Dim xDoc As XmlDocument = New XmlDocument()
        xDoc.Load("temp_content.xml")

        Dim FsdXML = XElement.Load("temp_content.xml")
        If FsdXML.Name.ToString = "freestyle" Then

            Dim TitleID As XmlNodeList = xDoc.GetElementsByTagName("id")
            Dim GameName As XmlNodeList = xDoc.GetElementsByTagName("title")

            For index = 0 To TitleID.Count - 1
                Form1.GamesListView.Items.Add(GameName(index).InnerText.ToString)
                Form1.GamesListView.Items(index).SubItems.Add(TitleID(index).InnerText.ToString.ToUpper)
            Next
            LogStuff.WriteMessage("Content.xml opened successfully")
            LogStuff.WriteMessage("Now select your titles and 'Get TU's'")
        Else
            LogStuff.ErrorLogWrite(xmlLocation, "is not a freestyle dash content.xml")
        End If


    End Sub

    Public Sub AddTUtoLog(ByVal TitleID As String, ByVal TUname As String)
        Dim xDoc As XmlDocument = New XmlDocument()

        Dim textWriter As XmlTextWriter = New XmlTextWriter("tulist.xml", Nothing)
        textWriter.WriteStartDocument()
        textWriter.WriteComment("Title Downloader update log")
        textWriter.WriteStartElement("Title_Downloader")
        textWriter.WriteStartElement("Game")
        textWriter.WriteStartElement("Game_Name")
        textWriter.WriteString(InputBox("value"))
        textWriter.WriteEndElement()
        textWriter.WriteStartElement("Title_ID")
        textWriter.WriteEndElement()

        textWriter.Close()

    End Sub
End Module
